package com.qk.management.config;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.qk.properties.AliOssProperties;
import com.qk.util.AliOssUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author:zhengbowen
 * @Date: 2025/08/31 21:21
 * @Description:
 */
@Configuration
@Slf4j
@EnableConfigurationProperties(AliOssProperties.class)
public class AilOssConfig {

    @Bean
    @ConditionalOnMissingBean
    public AliOssUtils aliOssUtil(AliOssProperties aliOssProperties) {
        log.info("开始创建阿里云文件上传工具类,{}",aliOssProperties);
        return new AliOssUtils(aliOssProperties.getEndpoint(),
                aliOssProperties.getAccessKeyId(),
                aliOssProperties.getAccessKeySecret(),
                aliOssProperties.getBucketName());
    }

    @Bean
    public OSS ossClient(AliOssProperties aliOssProperties) {
        // 创建并返回 OSS 实例，这个实例会被 Spring 容器管理
        return new OSSClientBuilder().build(aliOssProperties.getEndpoint(),
                aliOssProperties.getAccessKeyId(),
                aliOssProperties.getAccessKeySecret());
    }
}
